# 安装mysql
if [[ -z "$(docker images -q mysql:latest 2> /dev/null)" ]]; then
     docker pull mysql:latest
else
    echo "mysql installed"
fi

#	运行
  if [[ -z "$(docker ps -aqf "name=mysql-pattern")" ]]; then
    docker run -itd --name mysql-pattern --add-host=project-pattern:192.168.0.101  -p 3306:3306 --restart=always -e MYSQL_ROOT_PASSWORD=123456 mysql
    # 进入容器 修改加密方式
      docker exec -it mysql-pattern bash
      mysql -u root -p;
      use mysql;
      alter user 'root'@'%' identified with mysql_native_password by 'p123456';
  else
      echo "mysql-pattern running"
  fi

#用客户端连接上
  # 创建pattern库 执行.environment/init/db/pattern/init的脚本
  # 创建nacos_config库 执行.environment/init/db/nacos-mysql.sql的脚本
  # 创建xxl_job库 执行.environment/init/db/tables_xxl_job.sql的脚本
  # 创建seata库 执行.environment/init/db/seata.sql的脚本


#定时备份数据库
#mkdir -p /opt/mysql/202204
#docker exec -it mysql-pattern mysqldump -uroot -pp123456 -hproject-pattern pattern-c > /opt/mysql/202204/pattern-c.sql
#docker exec -it mysql-pattern mysqldump -uroot -pp123456 -hproject-pattern gitea > /opt/mysql/202204/gitea.sql
#docker exec -it mysql-pattern mysqldump -uroot -pp123456 -hproject-pattern nacos_config > /opt/202204/202203/nacos_config.sql
#docker exec -it mysql-pattern mysqldump -uroot -pp123456 -hproject-pattern seata > /opt/mysql/202204/seata.sql
#docker exec -it mysql-pattern mysqldump -uroot -pp123456 -hproject-pattern xxl_job > /opt/mysql/202204/xxl_job.sql

#show variables like '%max_connection%';
#set global max_connections=10000;